package seeders

import (
	"hyper/app/Models"
	"hyper/supports/database"
)

// 添加管理员角色关联表
func AdminRoleSeeder() {

	if !isAdminRoleExist() {
		for _, i := range getAllAdmin() {
			for _, v := range getAllRole() {
				_role_admin := []Models.AdminRole{
					{
						RoleId:  v.ID,
						AdminId: i.ID,
					},
				}
				database.DB.Create(&_role_admin)
			}
		}
	}
}

// 查找数据
func isAdminRoleExist() bool {
	var count int64
	database.DB.Model(&Models.AdminRole{}).Count(&count)
	return count > 0
}

// 获取所有管理员
func getAllAdmin() (admins []Models.Admin) {
	database.DB.Model(&Models.Admin{}).Find(&admins)
	return
}

// 获取所有的角色
func getAllRole() (roles []Models.Role) {
	database.DB.Model(&Models.Role{}).Find(&roles)
	return
}
